package pl.edu.agh.gronoMining.owlToGraphConverter.parser.implementation;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;

import org.apache.log4j.Logger;

import pl.edu.agh.gronoMining.owlToGraphConverter.graph.Graph;
import pl.edu.agh.gronoMining.owlToGraphConverter.parser.GraphParser;
import pl.edu.agh.gronoMining.owlToGraphConverter.parser.IExtendedSerializer;
import pl.edu.agh.gronoMining.owlToGraphConverter.parser.ILineParser;
import pl.edu.agh.gronoMining.owlToGraphConverter.parser.IOwlParser;
import pl.edu.agh.gronoMining.owlToGraphConverter.parser.ISimpleSerializer;
import pl.edu.agh.gronoMining.owlToGraphConverter.parser.utils.IConverter;
import pl.edu.agh.gronoMining.owlToGraphConverter.parser.utils.IDataManager;
import pl.edu.agh.gronoMining.owlToGraphConverter.parser.utils.implementation.Converter;
import pl.edu.agh.gronoMining.owlToGraphConverter.parser.utils.implementation.DataManager;


public class GronoOwlParser implements GraphParser {

	private static final String DATA_MANAGER_FILE_NAME = "dataManager.ser";
	
	private IDataManager dataManager;
	private IConverter converter;
	
	private IOwlParser owlParser;
	private ILineParser lineParser;
	private ISimpleSerializer simpleSerializer;
	private IExtendedSerializer extendedSerializer;
	
	private Logger logger = Logger.getLogger(GronoOwlParser.class);
	
	public GronoOwlParser() {
		
		this.dataManager = new DataManager();
		
		this.converter = new Converter();
		
		//this.owlParser = new OwlParser1(this.dataManager, this.converter);
		this.owlParser = new OwlParser2(this.dataManager, this.converter);
		this.lineParser = new LineParser1(this.dataManager, this.converter);
		//this.simpleSerializer = new SimpleSerializer1();
		this.simpleSerializer = new ToOneGraphSerializer();
		//this.simpleSerializer = new ToGSpanSerializer();
		this.extendedSerializer = new ExtendedSerializer1();
		
		this.owlParser.prepare();
		this.lineParser.prepare();
		
		logger.info("created");
	}

	@Override
	public Graph[] parseFromOwl(InputStream in) throws IOException, ParseException {

		Graph[] result = owlParser.parse(in);
		try {
			this.dataManager.serialize(DATA_MANAGER_FILE_NAME);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public Graph[] parseFromLine(InputStream in) throws IOException, ParseException {
		try {
			this.dataManager.deserialize(DATA_MANAGER_FILE_NAME);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		Graph[] result = lineParser.parse(in);
		
		try {
			this.dataManager.serialize(DATA_MANAGER_FILE_NAME);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return result;
	}

	@Override
	public void serializeToSimple(Graph[] graphs, OutputStream out) throws IOException {
		//for (Graph g : graphs) {
			simpleSerializer.serialize(graphs, out);
		//}
	}

	public void serializeToExtended(Graph[] graphs, OutputStream out) throws IOException {
		//for (Graph g : graphs) {
			extendedSerializer.serialize(graphs, out);
		//}
	}
}
